package com.hanagent.controller;
 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.alibaba.fastjson.JSONObject;
import com.hanagent.model.Agentorders;
import com.hanagent.model.Agents;
import com.hanagent.model.Postals;
import com.hanagent.myModel.GoodStock;
import com.hanagent.model.Moneyrecords;
import com.hanagent.utils.AgentStockUtils;
import com.hanagent.utils.ConstantUtils;
import com.hanagent.utils.DateUtils;
import com.hanagent.utils.MoneyUtils;
import com.hanagent.utils.ResponseUtils;
import com.hanagent.utils.StrUtils;
import com.hanagent.utils.UUidUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class AgentOrdersController extends Controller{
	
	public void getList() {
		TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8"); 
		TimeZone.setDefault(tz);
		int pageNumber = getParaToInt("pageNumber")<1?1:getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize")<1?1:getParaToInt("pageSize");
		String startCreateTime = getPara("startCreateTime");
		if (StrUtils.isEmpty(startCreateTime)) {
			startCreateTime = "1917-12-05 16:00:00";
		} 
		String endCreateTime = getPara("endCreateTime");
		if (StrUtils.isEmpty(endCreateTime)) {
			endCreateTime = "3017-12-05 16:00:00";
		}  
		String startUpdateTime = getPara("startUpdateTime");
		if (StrUtils.isEmpty(startUpdateTime)) {
			startUpdateTime = "1917-12-05 16:00:00";
		} 
		String endUpdateTime = getPara("endUpdateTime");
		if (StrUtils.isEmpty(endUpdateTime)) {
			endUpdateTime = "3017-12-05 16:00:00";
		} 
		
		String agentId = getPara("agentId");
		String orderType = getPara("orderType");
		String payType = getPara("payType");
		Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		int state = getParaToInt("state");
		if (StrUtils.isEmpty(agentId)) {
			if (loginAgents.getAgentLevel()<ConstantUtils.LevelCompanySource) { //公司查询所有订单
				Page<Agentorders> list = null;
				if (state==0) {//全部状态的订单
					if ((ConstantUtils.TypeBalance+"").equals(payType)) { 
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, "
								+ "AgentSale.agentName AS agentSaleName ", "from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = "
										+ "Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ?"
											+ " and AgentOrders.updatedAt < ? and AgentOrders.payType= ?" 
											+ " order by updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, "
								+ "AgentSale.agentName AS agentSaleName ", "from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = "
										+ "Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ?"
											+ " and AgentOrders.updatedAt < ? "
											+ "order by updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
					}
					
				}else{
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, "
								+ "AgentSale.agentName AS agentSaleName ", "from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = "
										+ "Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? "
											+ "and AgentOrders.updatedAt < ? and state = ?  and AgentOrders.payType=?  order by updatedAt desc", 
											startCreateTime, endCreateTime, startUpdateTime, endUpdateTime, state, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, "
								+ "AgentSale.agentName AS agentSaleName ", "from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = "
										+ "Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? "
											+ "and AgentOrders.updatedAt < ? and state = ? order by updatedAt desc", 
											startCreateTime, endCreateTime, startUpdateTime, endUpdateTime, state);
					}
				}
				addStaticData(loginAgents.getAgentId()+"", list);
			}else{ //其他代理查询自己的订单
				Page<Agentorders> list = null;
				if (state==0) {
					if("1".equals(orderType)){ //进货订单
						if ((ConstantUtils.TypeBalance+"").equals(payType)) {
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "
											+ "and AgentOrders.buyAgentId = " + loginAgents.getAgentId() + " and AgentOrders.payType=?" +
									" order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, startUpdateTime, endUpdateTime, payType);
						}else{
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + loginAgents.getAgentId() +
									" order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime);
						}
					}else if("0".equals(orderType)){ //销货
						if ((ConstantUtils.TypeBalance+"").equals(payType)) {
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "
											+ "and AgentOrders.saleAgentId = " + loginAgents.getAgentId() + " and AgentOrders.payType=? order by AgentOrders.updatedAt desc", 
											startCreateTime,  endCreateTime, startUpdateTime, endUpdateTime, payType);
						}else{
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "
											+ "and AgentOrders.saleAgentId = " 
											+ loginAgents.getAgentId() + " order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime);
						}
					}else{ //全部
						if ((ConstantUtils.TypeBalance+"").equals(payType)) {
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and (AgentOrders.saleAgentId = " 
											+ loginAgents.getAgentId() + " or AgentOrders.buyAgentId = " + loginAgents.getAgentId() + "  ) and AgentOrders.payType=? order by AgentOrders.updatedAt desc", 
											startCreateTime,  endCreateTime, startUpdateTime, endUpdateTime, payType);
						}else{
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and (AgentOrders.saleAgentId = " 
											+ loginAgents.getAgentId() + " or AgentOrders.buyAgentId = " 
											+ loginAgents.getAgentId() + "  ) order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime);
						}
					}
					
				}else{
					if("1".equals(orderType)){ //进货订单
						if ((ConstantUtils.TypeBalance+"").equals(payType)) {
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + loginAgents.getAgentId() +
									" and state = ?  and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime, state, payType);
						}else{
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + loginAgents.getAgentId() +
									" and state = ?  order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime, state);
						} 
					}else if("0".equals(orderType)){
						if ((ConstantUtils.TypeBalance+"").equals(payType)) {
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "+
									" and AgentOrders.saleAgentId = " + loginAgents.getAgentId() + " and state = ? and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime, state, payType);
						}else{
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "+
									" and AgentOrders.saleAgentId = " + loginAgents.getAgentId() + " and state = ?  order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime, state);
						} 
					}else{ //全部
						if ((ConstantUtils.TypeBalance+"").equals(payType)) {
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "+
									" and ( AgentOrders.saleAgentId = " + loginAgents.getAgentId() + " or AgentOrders.buyAgentId = " 
											+ loginAgents.getAgentId() + " ) and state = ? and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime, state, payType);
						}else{
							list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
									"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
											+ "where AgentOrders.createdAt > ? and "
											+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? "+
									" and ( AgentOrders.saleAgentId = " + loginAgents.getAgentId() + " or AgentOrders.buyAgentId = " 
											+ loginAgents.getAgentId() + " ) and state = ?  order by AgentOrders.updatedAt desc", startCreateTime,  endCreateTime, 
									startUpdateTime, endUpdateTime, state);
						}
					}
				}
				addStaticData(loginAgents.getAgentId()+"", list);
			}
		}else{
			if (loginAgents.getAgentLevel()>ConstantUtils.LevelCompanySource) { //代理商为创始代理商级别一下的 需要检测权限
				Agents agentsPower = Agents.dao.findById(agentId);
				if (agentsPower.getAgentParentId() != loginAgents.getAgentId() && agentsPower.getAgentSecondParentId() != loginAgents.getAgentId()) {
					renderText(ResponseUtils.sendResult(0, "", "你无权查看与你无关的代理商订单"));
					return;
				}
			}
			
			Page<Agentorders> list = null;
			if (state==0) {
				if("1".equals(orderType)){ //进货订单
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + agentId +
								" and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + agentId +
								" order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime);
					} 
				}else if("0".equals(orderType)){
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?" +
								" and AgentOrders.saleAgentId = " + agentId + " and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?" +
								" and AgentOrders.saleAgentId = " + agentId + " order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime);
					}
					
				}else {
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and  AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?" +
								" and ( AgentOrders.saleAgentId = " + agentId + " or AgentOrders.buyAgentId = " + agentId + " ) and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?" +
								" and ( AgentOrders.saleAgentId = " + agentId + " or AgentOrders.buyAgentId = " + agentId + " ) order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime);
					}
				}
			}else{
				if("1".equals(orderType)){ //进货订单
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + agentId +
								" and state = ? and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, state, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ? and AgentOrders.buyAgentId = " + agentId +
								" and state = ? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, state);
					}
				}else if("0".equals(orderType)){
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?"+
								" and AgentOrders.saleAgentId = " + agentId + " and state = ? and AgentOrders.payType=? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, state, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?"+
								" and AgentOrders.saleAgentId = " + agentId + " and state = ? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, state);
					} 
				}else {
					if ((ConstantUtils.TypeBalance+"").equals(payType)) {
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?"+
								" and ( AgentOrders.saleAgentId = " + agentId + " or  AgentOrders.buyAgentId = " + agentId + " ) and state = ? and AgentOrders.payType=?"
										+ " order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, state, payType);
					}else{
						list = Agentorders.dao.paginate(pageNumber, pageSize, "select AgentOrders.*, Agents.agentName AS agentBuyName, AgentSale.agentName AS agentSaleName ", 
								"from AgentOrders INNER JOIN Agents ON AgentOrders.buyAgentId = Agents.agentId  INNER JOIN Agents AgentSale ON AgentOrders.saleAgentId = AgentSale.agentId  "
								+ "where AgentOrders.createdAt > ? and "
								+ "AgentOrders.createdAt < ? and AgentOrders.updatedAt > ? and AgentOrders.updatedAt < ?"+
								" and ( AgentOrders.saleAgentId = " + agentId + " or  AgentOrders.buyAgentId = " + agentId + " ) and state = ? order by AgentOrders.updatedAt desc", startCreateTime,  
								endCreateTime, startUpdateTime, endUpdateTime, state);
					}
					
				}
			}
	        addStaticData(agentId, list);
		}
    }
	
	private void addStaticData(String agentId, Page<Agentorders> list) {
		String infoData = "";
		//查看指定代理商id的总业绩   已审核的销货订单
		Record allPriceSum = Db.findFirst("select sum(price) as allPrice from AgentOrders where saleAgentId="+ agentId + " and (state = " + ConstantUtils.StateOrderPass +
    			" or state = " + ConstantUtils.StateOrderSendGood + " or state = " + ConstantUtils.StateOrderTakeGood + ")");
		double allPrice = 0; 
		if (allPriceSum.get("allPrice")!=null) {
			allPrice =  allPriceSum.getDouble("allPrice");
		}
		DecimalFormat df1 = new DecimalFormat("0.0"); 
		infoData = "总业绩:" + df1.format(allPrice) + "元";
		
		double day7Pirice = getDatePrice(agentId, getPastDate(7), getPastDate(-1));
		infoData = infoData + "   最近7天业绩:" + df1.format(day7Pirice) + "元";
		
		double day30Pirice = getDatePrice(agentId, getPastDate(30), getPastDate(-1));
		infoData = infoData + "   最近30天业绩:" + df1.format(day30Pirice) + "元";
		
		String startCreateTime = getPara("startCreateTime");
		String endCreateTime = getPara("endCreateTime");
		if (!StrUtils.isEmpty(startCreateTime) && !StrUtils.isEmpty(endCreateTime)) {
			double datePrice = getDatePrice(agentId, startCreateTime.split(" ")[0], endCreateTime.split(" ")[0]);
			infoData = infoData + "   " + startCreateTime.split(" ")[0] +"至" + endCreateTime.split(" ")[0] +"业绩:" + df1.format(datePrice)+"元";
		}
		JSONObject object = new JSONObject();
		object.put("infoData", infoData);
		object.put("listData", list); 
		renderText(ResponseUtils.sendResult(1, object, "获取数据成功"));
	}
	
	private double getDatePrice(String agentId, String startCreateTime, String endCreateTime){
		Record datePriceSum = Db.findFirst("select sum(price) as datePrice from AgentOrders where saleAgentId="+ agentId + " and (state = " + ConstantUtils.StateOrderPass +
    			" or state = " + ConstantUtils.StateOrderSendGood + " or state = " + ConstantUtils.StateOrderTakeGood + ")"
				+" and AgentOrders.createdAt > '" + startCreateTime + "' and AgentOrders.createdAt < '" + endCreateTime +"'");
		double datePrice = 0; 
		if (datePriceSum.get("datePrice")!=null) {
			datePrice = datePriceSum.getDouble("datePrice");
		}
		return datePrice;
	}
	
	/** 
    * 获取过去第几天的日期 
    * 
    * @param past 
    * @return 
    */  
   public static String getPastDate(int past) {  
       Calendar calendar = Calendar.getInstance();  
       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
       Date today = calendar.getTime();  
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
       String result = format.format(today);  
       System.out.println("getPastDate result" + result);
       return result;  
   }  
	
	/**  
     */  
    public void addOrders() {  
    	Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
    	Agentorders orders = getModel(Agentorders.class, "");//别名  Jfinal 必杀技快速开发全靠它  
    	Agents buyAgents = Agents.dao.findById(orders.getBuyAgentId()); //进货的代理商
    	if (buyAgents==null) {
    		renderText(ResponseUtils.sendResult(0, "", "查找不到进货代理商")); 
    		return;
		}
    	
//    	if (buyAgents.getAgentLevel()==ConstantUtils.LevelTop && orders.getPrice()<30000 ) { //总代理级别 总代理进货数量必须3万以上
//    		renderText(ResponseUtils.sendResult(0, "", "总代理二次进货必须3万以上")); 
//    		return;
//		}
    	
    	Agents parentAgents = Agents.dao.findById(loginAgents.getAgentParentId()); //推荐他的代理商
    	if (loginAgents.getAgentLevel()==ConstantUtils.LevelTop) {
    		orders.setSaleAgentId(ConstantUtils.SuperAgentId); 
		}else{
			while (parentAgents.getAgentLevel()>ConstantUtils.LevelTop && parentAgents.getAgentLevel()>=loginAgents.getAgentLevel()){
	    		//如果父代理不为总代理或者公司  而且父代理级别不比新加代理高  则寻找到总代理后者公司为止
	        	parentAgents = Agents.dao.findFirst("select * from agents where agentId = ?", parentAgents.getAgentParentId());
	    	}
			orders.setSaleAgentId(parentAgents.getAgentId()); 
		}
    	orders.setAgentOrderId(UUidUtils.getUUid());
    	 
    	if (buyAgents.getAgentLevel()==ConstantUtils.LevelTop) { //只记录总代理的进货
    		String sourceAgentIds = buyAgents.getAgentId()+",";  //业绩统计的标识
        	Agents sourceAgents = Agents.dao.findById(loginAgents.getAgentParentId()); //查找源代理商
        	if (sourceAgents!=null) { 
        		sourceAgentIds = sourceAgentIds + sourceAgents.getAgentId() + ",";
        		while (sourceAgents.getAgentLevel()>ConstantUtils.LevelSuper) {
            		sourceAgents = Agents.dao.findById(sourceAgents.getAgentParentId()); //查找源代理商
            		if (sourceAgents!=null) {
            			sourceAgentIds = sourceAgentIds + sourceAgents.getAgentId() + ",";
            		}
        		}
    		}
     		orders.setAgentSourceIds(sourceAgentIds);
		}
    	
    	int payType = getParaToInt("payType");
        String payImg = getPara("payImg");
    	if (orders.getState()!=0 && StrUtils.isEmpty(payImg) && !(payType==ConstantUtils.TypePayCash || payType==ConstantUtils.TypeBalance)) {
        	renderText(ResponseUtils.sendResult(0, "", "非现金或余额支付方式支付截图不能为空")); 
    		return;
		}
    	if (payType==ConstantUtils.TypeEMoney) {//电子币  检查代理商电子币是否足够
			if (orders.getPrice()>buyAgents.getEMoney()) {
				renderText(ResponseUtils.sendResult(0, "", "电子币不足")); 
	    		return;
			}else {
				buyAgents.setEMoney(buyAgents.getEMoney()-orders.getPrice());
				buyAgents.update();
			}
		}else if (payType==ConstantUtils.TypeBalance) { //余额支付，检查代理商余额是否充足
			if (MoneyUtils.getCanPostal(loginAgents.getAgentId()) < orders.getPrice()) {
	    		renderText(ResponseUtils.sendResult(0, "", "余额不足")); 
	    		return;
			}else {
				Agents saleAgents = Agents.dao.findById(orders.getSaleAgentId()); //推荐他的代理商
				if (saleAgents.getAgentLevel()>ConstantUtils.LevelSuper) {
					//检查销货代理商的库存
		    		ArrayList<Agentorders> listAgentBuyOrders = (ArrayList<Agentorders>) Agentorders.dao.find("select * from AgentOrders where buyAgentId = "
		        			+ orders.getSaleAgentId() + " and (state = " + ConstantUtils.StateOrderPass +
		        			" or state = " + ConstantUtils.StateOrderSendGood + " or state = " + ConstantUtils.StateOrderTakeGood + ")");
		        	ArrayList<GoodStock> goodBuyStocks = AgentStockUtils.statisticsOrderGood(listAgentBuyOrders);
		          
		        	ArrayList<Agentorders> listAgentSaleOrders = (ArrayList<Agentorders>) Agentorders.dao.find("select * from AgentOrders where saleAgentId = "
		        			+ orders.getSaleAgentId()  + " and (state = " + ConstantUtils.StateOrderPass +
		        			" or state = " + ConstantUtils.StateOrderSendGood + " or state = " + ConstantUtils.StateOrderTakeGood + ")");
		        	listAgentSaleOrders.add(orders);
		        	ArrayList<GoodStock> goodSaleStocks = AgentStockUtils.statisticsOrderGood(listAgentSaleOrders);
		        	AgentStockUtils.addOrderDatas(goodSaleStocks, orders.getGoodDatas());
		        	
		        	if (!AgentStockUtils.compareStock(goodBuyStocks, goodSaleStocks)) {
		        		renderText(ResponseUtils.sendResult(0, "", "销货商库存不足，请先进货"));
		    			return;
		    		}
				}
				if (saleAgents.getAgentLevel()==ConstantUtils.LevelSuper) { //当为公司销货 设置订单为公司业绩   订单可分红
					orders.setShareState(0);
				}
				orders.setState(ConstantUtils.StateOrderPass);  //余额支付直接通过
				//添加销货代理商收益记录   
				String desc = "代理商"+buyAgents.getAgentMan()+"的进货款";
            	addRecord(ConstantUtils.MonneyTypeBalancePay, orders.getPrice(), orders.getAgentOrderId(), desc, orders.getSaleAgentId());
            	
            	calMoney(buyAgents, orders);
            	
            	//添加进货代理商提现记录
            	Postals postals = new Postals();
            	postals.setPostalMoney(orders.getPrice().intValue());
            	postals.setUserId(orders.getBuyAgentId());
            	postals.setUserType(2);
            	postals.setReviewedUserId(0);
            	postals.setReviewedUserName("");
            	postals.setEMoneyMan(parentAgents.getAgentMan());
            	postals.setEMoneyPhone(parentAgents.getAgentPhone());
            	postals.setState(ConstantUtils.StateAgentPass);
            	postals.setReviewReason("代理商进货，自动审核通过"); 
            	postals.setPostalType(ConstantUtils.TypePostalBalancePay); 
            	postals.save();
			}
		}
        boolean flag = orders.save();  
        if (flag) { 
        	renderText(ResponseUtils.sendResult(1, "", "添加成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "添加失败"));  
        }  
    }
    
    //审核代理商订单
    public void reviewAgentOrder(){
    	Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
    	Agentorders agentOrders = getModel(Agentorders.class, "");//别名  Jfinal 必杀技快速开发全靠它 
    	Agents buyAgents = Agents.dao.findById(agentOrders.getBuyAgentId()); //进货的代理商
    	Agents saleAgents = Agents.dao.findById(agentOrders.getSaleAgentId()); //销货的代理商
    	
    	int loginAgentID = loginAgents.getAgentId();
    	int saleAgentID = saleAgents.getAgentId();
    	int buyAgentID = buyAgents.getAgentId();
    	int orderState = agentOrders.getState();
    	
    	Agentorders sourceAgentorders = agentOrders.findById(agentOrders.getTableId());
    	if (orderState==sourceAgentorders.getState()) {
    		renderText(ResponseUtils.sendResult(0, "", "订单状态一样  请勿重复提交")); 
    		return;
		}
    	
    	if (orderState==ConstantUtils.StateOrderRefused) {//驳回
    		if (loginAgentID != saleAgentID) {
        		renderText(ResponseUtils.sendResult(0, "", "只有销货代理商才可驳回"));
    			return;
    		}
    		String reviewReason = getPara("reviewReason");
    		if (StrUtils.isEmpty(reviewReason)) {
    			renderText(ResponseUtils.sendResult(0, "", "驳回请输入驳回原因")); 
    			return;
			}
    		boolean flag = agentOrders.findById(agentOrders.getTableId()).set("state", ConstantUtils.StateOrderRefused).set("shareState", 2)
    				.set("updatedAt", DateUtils.getNowDateWith8()).set("reviewReason", getPara("reviewReason")).update();
    		if (flag) {
    			//如果不是普通进货订单则查找进货代理商是否为待审核状态
            	if (agentOrders.getOrderType()!=ConstantUtils.TypeAgentOrderCustom && buyAgents.getReviewedState()==ConstantUtils.StateAgentWaitReView) {
    				buyAgents.setReviewedState(ConstantUtils.StateAgentRefused);
    				buyAgents.setStateInfo(getPara("reviewReason"));
    				buyAgents.update();
    			}
            	int payType = getParaToInt("payType");
            	if (payType==ConstantUtils.TypeEMoney) {//电子币  驳回须恢复电子币
            		buyAgents.setEMoney(buyAgents.getEMoney()+agentOrders.getPrice()); 
            		buyAgents.update();
				}
    			renderText(ResponseUtils.sendResult(1, "", "驳回成功")); 
			}else{
				renderText(ResponseUtils.sendResult(0, "", "驳回失败，请重试")); 
			}
    		return;
		}
    	
    	if (orderState==ConstantUtils.StateOrderSendGood) {//发货
    		if (loginAgentID != saleAgentID) {
        		renderText(ResponseUtils.sendResult(0, "", "只有销货代理商才可发货"));
    			return;
    		}
    		String reviewReason = getPara("reviewReason");
    		if (StrUtils.isEmpty(reviewReason)) {
    			renderText(ResponseUtils.sendResult(0, "", "发货请在留言中输入物流信息")); 
    			return;
			}
    		
    		boolean flag = agentOrders.findById(agentOrders.getTableId()).set("state", ConstantUtils.StateOrderSendGood)
    				.set("updatedAt", DateUtils.getNowDateWith8()).set("reviewReason", getPara("reviewReason")).update();
    		if (flag) {
    			renderText(ResponseUtils.sendResult(1, "", "发货成功")); 
			}else{
				renderText(ResponseUtils.sendResult(0, "", "发货失败，请重试")); 
			}
    		return;
		}
    	
    	if (orderState==ConstantUtils.StateOrderTakeGood) {//收货
    		if (loginAgentID != buyAgentID) {
        		renderText(ResponseUtils.sendResult(0, "", "只有进货商才可收货"));
    			return;
    		}
    		boolean flag = agentOrders.findById(agentOrders.getTableId()).set("state", ConstantUtils.StateOrderTakeGood)
    				.set("reviewReason", getPara("reviewReason")).set("updatedAt", DateUtils.getNowDateWith8()).update();
    		if (flag) {
    			renderText(ResponseUtils.sendResult(1, "", "收货成功")); 
			}else{
				renderText(ResponseUtils.sendResult(0, "", "收货失败，请重试")); 
			}
    		return;
		}
    	
    	if (loginAgents.getAgentLevel()>ConstantUtils.LevelSuper) {
    		//检查销货代理商的库存
    		ArrayList<Agentorders> listAgentBuyOrders = (ArrayList<Agentorders>) Agentorders.dao.find("select * from AgentOrders where buyAgentId = "
        			+ loginAgentID + " and (state = " + ConstantUtils.StateOrderPass +
        			" or state = " + ConstantUtils.StateOrderSendGood + " or state = " + ConstantUtils.StateOrderTakeGood + ")");
        	ArrayList<GoodStock> goodBuyStocks = AgentStockUtils.statisticsOrderGood(listAgentBuyOrders);
          
        	ArrayList<Agentorders> listAgentSaleOrders = (ArrayList<Agentorders>) Agentorders.dao.find("select * from AgentOrders where saleAgentId = "
        			+ loginAgentID  + " and (state = " + ConstantUtils.StateOrderPass +
        			" or state = " + ConstantUtils.StateOrderSendGood + " or state = " + ConstantUtils.StateOrderTakeGood + ")");
        	ArrayList<GoodStock> goodSaleStocks = AgentStockUtils.statisticsOrderGood(listAgentSaleOrders);
        	AgentStockUtils.addOrderDatas(goodSaleStocks, agentOrders.getGoodDatas());
        	
        	if (!AgentStockUtils.compareStock(goodBuyStocks, goodSaleStocks)) {
        		renderText(ResponseUtils.sendResult(0, "", "库存不足，请先进货"));
    			return;
    		}
		}
    	boolean flag = false;
    	if (saleAgents.getAgentLevel()==ConstantUtils.LevelSuper) { //当为公司销货 设置订单为公司业绩   订单可分红
    		flag = agentOrders.findById(agentOrders.getTableId()).set("state", orderState).set("shareState", 0)
    				.set("reviewReason", getPara("reviewReason")).set("updatedAt", DateUtils.getNowDateWith8()).update(); 
		}else{
			flag = agentOrders.findById(agentOrders.getTableId()).set("state", orderState).set("shareState", 2)
					.set("reviewReason", getPara("reviewReason")).set("updatedAt", DateUtils.getNowDateWith8()).update();
		}
        if (flag) { 
        	//如果不是普通进货订单则查找进货代理商是否为待审核状态
        	if (agentOrders.getOrderType()!=ConstantUtils.TypeAgentOrderCustom && buyAgents.getReviewedState()==ConstantUtils.StateAgentWaitReView) {
				buyAgents.setReviewedState(ConstantUtils.StateAgentPass);
				buyAgents.setAgentLevel(buyAgents.getAgentUpdateLevel());
				int agentPrice = agentOrders.getPrice().intValue();
				if (agentPrice>=500000){
					buyAgents.setAgentPrice(500000); //可分红金额最多50万
				}else{
					buyAgents.setAgentPrice(agentPrice);
				}
				buyAgents.update();
			}
        	
        	if (agentOrders.getPayType()==ConstantUtils.TypeEMoney) { 
        		saleAgents.setEMoney(saleAgents.getEMoney()+agentOrders.getPrice());
        		saleAgents.update();
			}
        
        	calMoney(buyAgents, agentOrders);
        	renderText(ResponseUtils.sendResult(1, "", "审核通过成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "审核通过失败，请重试"));  
        }
    }
    
    //计算收益
    private void calMoney(Agents buyAgents, Agentorders agentOrders) {
    	Agents parentAgents = Agents.dao.findById(buyAgents.getAgentParentId()); //进货的代理商 的推荐代理商
    	//添加成功后  需要纪录订单的各个数据
		if (parentAgents.getAgentLevel()>=buyAgents.getAgentLevel() && parentAgents.getAgentLevel()<ConstantUtils.LevelThird) {
        	if (parentAgents.getAgentLevel()==ConstantUtils.LevelFirst) {
            	double moneyCount = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.97);
    			//审核通过后  需要记录一下
            	String desc = "一级代理("+parentAgents.getAgentMan()+")推荐代理商一代("+ buyAgents.getAgentMan() +")进货款的5%";
            	addRecord(ConstantUtils.MonneyTypeSaleFirstAgent10, moneyCount, agentOrders.getAgentOrderId(), desc, parentAgents.getAgentId());
            	
            	double moneyCount1 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.01);
				String desc1 = desc+"的1%爱心基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeAixin, moneyCount1, agentOrders.getAgentOrderId(), desc1, parentAgents.getAgentId());
            	
            	double moneyCount2 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.02);
				String desc2 = desc+"的2%旅游基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeLvYou, moneyCount2, agentOrders.getAgentOrderId(), desc2, parentAgents.getAgentId());
            	
			}else if(parentAgents.getAgentLevel()==ConstantUtils.LevelTop){
            	double moneyCount = MoneyUtils.formatDouble(agentOrders.getPrice()*0.1*0.97);
    			//审核通过后  需要记录一下
            	String desc = "总代理("+parentAgents.getAgentMan()+")推荐代理商一代("+ buyAgents.getAgentMan() +")进货款的10%";
            	addRecord(ConstantUtils.MonneyTypeSaleFirstAgent10, moneyCount, agentOrders.getAgentOrderId(), desc, parentAgents.getAgentId());
            	
            	double moneyCount1 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.1*0.01);
				String desc1 = desc+"的1%爱心基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeAixin, moneyCount1, agentOrders.getAgentOrderId(), desc1, parentAgents.getAgentId());
            	
            	double moneyCount2 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.1*0.02);
				String desc2 = desc+"的2%旅游基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeLvYou, moneyCount2, agentOrders.getAgentOrderId(), desc2, parentAgents.getAgentId());
			}else if(parentAgents.getAgentLevel()==ConstantUtils.LevelSecond){
            	double moneyCount = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.97);
    			//审核通过后  需要记录一下
            	String desc = "二级代理("+parentAgents.getAgentMan()+")推荐代理商一代("+ buyAgents.getAgentMan() +")进货款的5%";
            	addRecord(ConstantUtils.MonneyTypeSaleFirstAgent10, moneyCount, agentOrders.getAgentOrderId(), desc, parentAgents.getAgentId());
            	
            	double moneyCount1 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.01);
				String desc1 = desc+"的1%爱心基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeAixin, moneyCount1, agentOrders.getAgentOrderId(), desc1, parentAgents.getAgentId());
            	
            	double moneyCount2 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.02);
				String desc2 = desc+"的2%旅游基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeLvYou, moneyCount2, agentOrders.getAgentOrderId(), desc2, parentAgents.getAgentId());
			}
        	
		}
		Agents secParentAgents = Agents.dao.findById(parentAgents.getAgentParentId()); //进货的代理商 的推荐代理商 的推荐代理商
		if (secParentAgents != null && secParentAgents.getAgentLevel()>=buyAgents.getAgentLevel()) {
			if (secParentAgents.getAgentLevel()==ConstantUtils.LevelFirst) {
				double moneyCount = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.97);
    			//审核通过后  需要记录一下
            	String desc = "一级代理("+secParentAgents.getAgentMan()+")推荐代理商二代("+ buyAgents.getAgentMan() +")进货款的5%";
            	addRecord(ConstantUtils.MonneyTypeSaleSecAgent5, moneyCount, agentOrders.getAgentOrderId(), desc, secParentAgents.getAgentId());
            	
            	double moneyCount1 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.01);
				String desc1 = desc+"的1%爱心基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeAixin, moneyCount1, agentOrders.getAgentOrderId(), desc1, secParentAgents.getAgentId());
            	
            	double moneyCount2 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.02);
				String desc2 = desc+"的2%旅游基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeLvYou, moneyCount2, agentOrders.getAgentOrderId(), desc2, secParentAgents.getAgentId());
			}else if(secParentAgents.getAgentLevel()==ConstantUtils.LevelTop){
				double moneyCount = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.97);
				String desc = "总代理("+secParentAgents.getAgentMan()+")推荐代理商二代("+ buyAgents.getAgentMan() +")进货款的5%";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeSaleSecAgent5, moneyCount, agentOrders.getAgentOrderId(), desc, secParentAgents.getAgentId());
            	
            	double moneyCount1 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.01);
				String desc1 = desc+"的1%爱心基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeAixin, moneyCount1, agentOrders.getAgentOrderId(), desc1, secParentAgents.getAgentId());
            	
            	double moneyCount2 = MoneyUtils.formatDouble(agentOrders.getPrice()*0.05*0.02);
				String desc2 = desc+"的2%旅游基金";
    			//审核通过后  需要记录一下 
            	addRecord(ConstantUtils.MonneyTypeLvYou, moneyCount2, agentOrders.getAgentOrderId(), desc2, secParentAgents.getAgentId());
			}
		}
	}
    
    //添加爱心基金   旅游基金  提现手续费记录
    private void addRecord(int type, double count, String fromId, String desc, int agentId) {
    	new Moneyrecords().set("type", type).set("moneyCount", count).set("fromId", fromId).set("desc", desc)
    	.set("userOrAgentId", agentId).set("userType", "2").save();
	}
    
    
//    /** 
//     * 修改 
//     */   
//    public void updateOrders() {  
//    	AgentOrders orders = getModel(AgentOrders.class, "");//别名  Jfinal 必杀技快速开发全靠它  
//        boolean flag = orders.update();
//        if (flag) {  
//        	renderText(ResponseUtils.sendResult(1, "", "修改成功"));  
//        }else {  
//        	renderText(ResponseUtils.sendResult(0, "", "修改失败"));  
//        }   
//    }  
//      
//    /** 
//     * 删除单个 
//     */  
//    public void delOrdersById() {  
//        Integer id = getParaToInt("agentOrderId");  
//        boolean flag = AgentOrders.dao.deleteById(id);  
//        if (flag) {  
//        	renderText(ResponseUtils.sendResult(1, "", "删除成功"));  
//        }else {  
//        	renderText(ResponseUtils.sendResult(0, "", "删除失败"));  
//        }    
//    } 

}
